package com.hjj.test.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * mybatis plus 配置
 */
@EnableTransactionManagement
@Configuration
@MapperScan("com.hjj.**.service.*.mapper") //扫描指定mapper接口包名注入bean
public class MybatisPlusConfig {

	/**
	 * 乐观锁
	 * 分页插件
	 */
	@Bean
	public MybatisPlusInterceptor mybatisPlusInterceptor() {
		MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
		// 分页插件
		// 如果配置多个插件, 切记分页最后添加
		// 如果有多数据源可以不配具体类型, 否则都建议配上具体的 DbType
		interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));

		// 乐观锁
		interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());

		// 防止全表更新与删除
		interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());

		return interceptor;
	}


	/**
	 * SQL执行效率插件(用于输出每条 SQL 语句及其执行时间)
	 */
//	@Bean
//	@Profile({ProfileConstants.PROFILE_DEV,ProfileConstants.PROFILE_TEST})// 设置 dev test 环境开启
//	public PerformanceInterceptor performanceInterceptor() {
//		return new PerformanceInterceptor();
//	}
}
